#!/bin/bash

ACME_DIR="$(pwd)"

# Fetch boulder.
travis_retry go get \
  bitbucket.org/liamstask/goose/cmd/goose \
  github.com/jsha/listenbuddy \
  github.com/letsencrypt/boulder/... \
  github.com/tcnksm/ghr \
  github.com/hlandau/gox

# Setup DB.
cd $GOPATH/src/github.com/letsencrypt/boulder
./test/create_db.sh || exit 2

# Setup AMQP.
go run cmd/rabbitmq-setup/main.go -server amqp://localhost || exit 4

# Start boulder.
patch -t -p1 < "$ACME_DIR/.travis/boulder.patch" || exit 3
export OBJDIR="$GOPATH/src/github.com/letsencrypt/boulder/bin"
./start.py &
START_PID=$$

# Wait for boulder to come up.
echo Waiting for boulder to come up...
while ((1)); do
  kill -0 "$START_PID" || break
  [ -e /tmp/boulder-has-started ] && break

  sleep 1
done
echo Boulder up.
echo ----------------------------------------------------------------

# Run tests.
cd "$ACME_DIR"

go test -v ./...
RESULT=$?

echo Done with exit code $RESULT
if [ "$RESULT" != "0" ]; then
  exit $RESULT
fi

# Test cross-compilation. The binaries produced are also used for release
# upload in after_success if this is a release tag.

echo Cross-compiling releases...
mkdir -p "$GOPATH/releasing/idist" "$GOPATH/releasing/dist"

# Assume that x86 machines don't necessarily have SSE2. Whereas for amd64,
# require SSE2.

REPO=github.com/$TRAVIS_REPO_SLUG
BINARIES=$REPO/cmd/acmetool
export BUILDNAME="by travis"
BUILDINFO="$($GOPATH/src/github.com/hlandau/degoutils/buildinfo/gen $BINARIES)"

# cgo crosscompile
gox -ldflags "$BUILDINFO" -cgo -osarch 'linux/amd64' -output "$GOPATH/releasing/idist/acmetool-$TRAVIS_TAG-{{.OS}}_{{.Arch}}_cgo/bin/{{.Dir}}" $BINARIES
RESULT1=$?
GO386=387 gox -ldflags "$BUILDINFO" -cgo -osarch 'linux/386' -output "$GOPATH/releasing/idist/acmetool-$TRAVIS_TAG-{{.OS}}_{{.Arch}}_cgo/bin/{{.Dir}}" $BINARIES
RESULT2=$?

# non-cgo crosscompile
gox -ldflags "$BUILDINFO" -osarch 'darwin/amd64 linux/amd64 linux/arm linux/arm64 freebsd/amd64 freebsd/arm openbsd/amd64 netbsd/amd64 netbsd/arm dragonfly/amd64 solaris/amd64' -output "$GOPATH/releasing/idist/acmetool-$TRAVIS_TAG-{{.OS}}_{{.Arch}}/bin/{{.Dir}}" $BINARIES
RESULT3=$?
GO386=387 gox -ldflags "$BUILDINFO" -osarch 'linux/386 darwin/386 freebsd/386 openbsd/386 netbsd/386' -output "$GOPATH/releasing/idist/acmetool-$TRAVIS_TAG-{{.OS}}_{{.Arch}}/bin/{{.Dir}}" $BINARIES
RESULT4=$?

# Defer exiting to get as much error output as possible upfront.
echo "cgo crosscompile (amd64) exited with code $RESULT1"
echo "cgo crosscompile (386) exited with code $RESULT2"
echo "non-cgo crosscompile (amd64) exited with code $RESULT3"
echo "non-cgo crosscompile (386) exited with code $RESULT4"

if [ "$RESULT1" != "0" ]; then
  exit $RESULT1
fi
if [ "$RESULT2" != "0" ]; then
  exit $RESULT2
fi
if [ "$RESULT3" != "0" ]; then
  exit $RESULT3
fi
if [ "$RESULT4" != "0" ]; then
  exit $RESULT4
fi

# No point stopping boulder, travis will do it.
# Don't exit here, we need after_success to run and this script is sourced.
